<?php
namespace Acme\Likipe\BlogBundle\Repository;

use Doctrine\ORM\EntityRepository;

/*
 * Acme\Likipe\BlogBundle\Repository\BlogRepository
 */

class BlogRepository extends EntityRepository{
	
	public function findUnderMaximum( $iMax ){
		$sQuery = $this->getEntityManager()
			->createQueryBuilder()
			->select( 'b' )
			->from( 'AcmeLikipeBlogBundle:Blog', 'b' )
			->where( 'b.maximum < :maximum' )
			->setParameter( 'maximum', $iMax )
			->orderBy( 'b.created', 'DESC' )
			->getQuery();
		
		return $sQuery->getResult();
	}
	
	public function findTopPosts( $iLimit ){
		$sQuery = $this->getEntityManager()->createQueryBuilder()
			->select( 'b.id', 'b.title', 'b.created', 'b.maximum', 
					'count(p.blog) as countPost' )
			->from( 'AcmeLikipeBlogBundle:Blog', 'b' )
			->from( 'AcmeLikipeBlogBundle:Post', 'p' )
			->where( 'b.id = p.blog' )
			->orderBy( 'countPost', 'DESC' )
			->groupBy( 'p.blog' )
			->setFirstResult( 0 )
			->setMaxResults( $iLimit )
			->getQuery();
		
		return $sQuery->getResult();
	}
}
?>